package com.hzqy.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
	/**
	 * 加（或者减）天数，返回计算后获得的时间
	 *
	 * @param date
	 * @param num()
	 * @return
	 */
	public static Date getAnotherDate(Date date, int num) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, num);
		return c.getTime();
	}
	/**
	 * 加（或者减）月数，返回计算后获得的时间
	 *
	 * @param date
	 * @param num()
	 * @return
	 */
	public static Date getAnotherMonth(Date date, int num) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, num);
		return c.getTime();
	}

	/**
	 * 获取月份最后一天
	 * @param date
	 * @return
	 */
	public static Date getMonthLastDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
		/*
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 先设置为下个月第一天
		calendar.add(Calendar.MONTH, 1);// 加一个月
		calendar.set(Calendar.DATE, 1); // 设置为该月第一天
		calendar.add(Calendar.DATE, -1);// 再减一天即为上个月最后一天*/
		// 设置时分秒
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	//取一天的最后时候
	public static Date getLastTimeOfDay(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设置时分秒
		calendar.set(Calendar.HOUR, 11);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	/**
	 * 获取今天为几号
	 * @param date
	 * @return
	 */
	public static String getMonthLastDatedd(Date date) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ss=sf.format(date);

		return ss.substring(8, 10);
	}

	/**
	 * 获取时间小时数
	 * @param date
	 * @return
	 */
	public static int getDateHour(Date date) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ss=sf.format(date);
		return Integer.parseInt(ss.substring(11, 13));
	}

	/**
	 * 获取月份的第一天
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 * @throws ParseException
	 */
	public static Date getMonthFristDate(Date date) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
		String ss=sf.format(date)+"-01 00:00:00";
		SimpleDateFormat sfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sfs.parse(ss);
	}

	/**
	 * 获取当天0点的时间
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 * @throws ParseException
	 */
	public static Date getCurrentDateZero(Date date) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String ss=sf.format(date)+" 00:00:00";
		SimpleDateFormat sfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sfs.parse(ss);
	}

	/**
	 * 返回周内的指定时间
	 *
	 * @param date 指定时间
	 * @param weekDiffer 前后周
	 * @param daySpecial 周几
	 * @return
	 */
	public static Date getAnotherWeekDay(Date date, int weekDiffer,int daySpecial,int firstWeek) {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.setFirstDayOfWeek(firstWeek);//设置周的起始时间
		calendar.setTime(date);
		calendar.set(Calendar.WEEK_OF_MONTH,calendar.get(Calendar.WEEK_OF_MONTH) + weekDiffer);
		calendar.get(Calendar.DAY_OF_WEEK);
		calendar.set(Calendar.DAY_OF_WEEK, daySpecial);
		return calendar.getTime();
	}
	/**
	 * 获取指定时间是星期几
	 * @return
	 */
	public static int getNowWeek(Date date){
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.setTime(date);
		int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
		return weekDay;
	}
	/**
	 * 获取指定时间是星期几 返回String
	 * @return
	 */
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}


	public static void main(String[] args) {
		Date date = PublicUtil.getDateFormat("2014-12-02", "yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		System.out.println(c.get(Calendar.MONTH));
		System.out.println(PublicUtil.getDateString(getAnotherMonth(getMonthLastDate(date),1), "yyyy-MM-dd HH:mm:ss") );
	}

	/**
	 * 返回指定格式的日期
	 *
	 * @param date 指定时间
	 * @param geshi 想要得到的格式
	 * @return	指定格式的日期
	 */
	public static String getDateString(Date date,String geshi){
		SimpleDateFormat sf = new SimpleDateFormat(geshi);
		String ss=sf.format(date);
		return ss;
	}
}
